package hu.diploma.android;

import hu.diploma.android.util.ServiceProviderImpl;
import hu.diploma.common.resource.UserResource;
import hu.diploma.common.vos.UserVo;

import org.restlet.resource.ClientResource;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.login);
		getResources().getString(R.string.regstration);
		final SharedPreferences settings = getSharedPreferences(ServiceProviderImpl.PREFS_NAME, MODE_PRIVATE);

		final EditText userName = (EditText) findViewById(R.id.login_user_name);
		userName.setText(settings.getString("user_name", null));

		final EditText password = (EditText) findViewById(R.id.login_password);
		password.setText(settings.getString("password", null));

		final CheckBox savePasswors = (CheckBox) findViewById(R.id.save_password);
		savePasswors.setChecked(settings.getBoolean("save_password", false));

		Button regButton = (Button) findViewById(R.id.reg_button);
		regButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				startActivityForResult(new Intent(Login.this, Registration.class), 0);
			}
		});

		Button loginButton = (Button) findViewById(R.id.start_login_button);
		loginButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Boolean result = null;
				String passwordStr = password.getText().toString();
				String userNameStr = userName.getText().toString();

				UserVo user = new UserVo();
				user.setName(userNameStr);
				user.setPassword(passwordStr);

				try {
					ClientResource cr = new ClientResource("http://192.168.1.65:8888/restlet/user");

					UserResource resource = cr.wrap(UserResource.class);

					result = resource.login(user);

				} catch (Exception e) {
					Toast.makeText(Login.this, R.string.login_error, Toast.LENGTH_LONG).show();
				}
				if (result != null) {
					if (result) {

						SharedPreferences.Editor editor = settings.edit();
						editor.putString("user_name", userNameStr);

						if (savePasswors.isChecked()) {
							editor.putString("password", passwordStr);
						} else {
							editor.putString("password", null);
						}

						editor.putBoolean("save_password", savePasswors.isChecked());
						editor.commit();

						ServiceProviderImpl.getInstance().setUser(user);

						setResult(RESULT_OK, getIntent());
						finish();

					} else {
						Toast.makeText(Login.this, R.string.login_fail, Toast.LENGTH_LONG).show();
					}
				}
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				setResult(RESULT_OK, getIntent());
				finish();
			}
		}
	}
}